﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace wpfProjects.Data
{
    /// <summary>
    /// 项目条目--视图定义
    /// </summary>
    public class ProjectItemView
    {
        public String ID { get; set; }
        public String Name { get; set; }

        public String Color { get; set; }
        public int Progress { get; set; }
        public String Description { get; set; }
    }


    /// <summary>
    ///  项目信息--视图定义
    /// </summary>
    public class ProjectView
    {
        public String ID { get; set; }
        public String ParentID { get; set; }
        public String Name { get; set; }
        public int Progress  { get; set; }
        public String Color { get; set; }
        public String Description { get; set; }

        public List<ProjectItemView> SubProjectView { get; set; }

        public ProjectView(ProjectView other)
        {
            ID = other.ID;
            Name = other.Name;
            ParentID = other.ParentID;
            Progress = other.Progress;
            Color = other.Color;
            Description = other.Description;
            SubProjectView = other.SubProjectView;
        }

        public ProjectView()
        {
            ID = "";
            ParentID = "";
            Name = "";
            Progress = 0;
            Color = "black";
            Description = "";
            SubProjectView = new List<ProjectItemView>();
        }
    }

    public class RequestState
    {
        public WebRequest request {get; set;}
        public WebResponse response { get; set; }
    }

    public class DataInterface
    {
        public ProjectView CurrentProjectView { get; set; }
        public String URI { get; set; }

        public DataInterface()
        {
            URI = "http://localhost:8080/Project/";
        }

        public ProjectView GetTestProjectView() 
        {
            CurrentProjectView = new ProjectView();
            CurrentProjectView.Name = "test01";
            CurrentProjectView.Progress = 10;
            CurrentProjectView.Description = "测试工程1";

            ProjectItemView subProjectView = new ProjectItemView();
            subProjectView.Name = "sub01";
            subProjectView.Progress = 10;
            subProjectView.Color = "yellow";
            subProjectView.Description="测试子工程1";
            CurrentProjectView.SubProjectView.Add(subProjectView);

            subProjectView = new ProjectItemView();
            subProjectView.Name = "sub02";
            subProjectView.Progress = 30;
            subProjectView.Color = "black";
            subProjectView.Description = "测试子工程2";

            CurrentProjectView.SubProjectView.Add(subProjectView);

            return CurrentProjectView;
        }

        public Boolean GetProjectView(String ID)
        {
            try
            {
                System.Uri uri = new Uri(URI+ID);

                WebClient webClient = new WebClient();

                webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
                webClient.OpenReadAsync(uri);
            }
            catch (Exception e)
            {
                int i = 0;
            }           

            return true;
        }

        void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Stream responseStream = e.Result;
               
                XmlReader responseReader = XmlReader.Create(responseStream);

                XElement user = XElement.Load(responseReader);
                int i = 0;
            }
        }

        public Boolean AddProjectView(ProjectView projectView)
        {
            return true;
        }

        public Boolean DelProjectView(String ID)
        {
            return true;
        }

        public Boolean SetProjectView(ProjectView projectView)
        {
            return true;
        }
    }
}
